package com.retailshop.shopuser.UserTask;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.retailshop.shopuser.common.InterfaceVer;
import com.retailshop.shopuser.common.MessageConstants;
import com.retailshop.shopuser.common.Response;
import com.retailshop.shopuser.entity.RedisUserNo;
import com.retailshop.shopuser.service.HsUserService;
import com.retailshop.shopuser.service.UserNoService;
import com.retailshop.shopuser.service.UserRedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

@Controller
@RequestMapping(value = "/User")
public class UserRedisAuto {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    UserRedisService userRedisService;

    //缓存用户数据
    @ResponseBody
    @RequestMapping(value = "/setRedisUserAll")
    public Response setRedisUserAll(HttpServletRequest request, @RequestBody JSONObject jsonParam) {
        Map<String, Object> map = InterfaceVer.Security(request, jsonParam);
        if (!map.get("code").toString().equals("000000")) {
            return new Response(MessageConstants.MSG_ACCESS_DENY, null, "", request);
        } else {
            try {
                Boolean bl = userRedisService.setRedisUserAll();
                return new Response(MessageConstants.MSG_SUCCESS, bl, 1, "", request);
            } catch (Exception ex) {
                return new Response(MessageConstants.MSG_ACCESS_DENY, null, 0, "缓存失败", request);
            }
        }
    }

    //缓存用户数据
    @ResponseBody
    @RequestMapping(value = "/setRedisUserAll1")
    public Response setRedisUserAll1(HttpServletRequest request) {
        Boolean bl = userRedisService.setRedisUserAll();
        return new Response(MessageConstants.MSG_SUCCESS, bl, 1, "", request);
    }

    //创建缓存用户数据
    @ResponseBody
    @RequestMapping(value = "/setRedisUser")
    public Response setRedisUser(HttpServletRequest request, @RequestParam Map<String, Object> map) {
        int id = 0;
        try {
            id = Integer.parseInt(map.get("key").toString());
        } catch (Exception ex) {
        }
        if (id > 0) {
            Boolean bl = userRedisService.setRedisUser(id);
            return new Response(MessageConstants.MSG_SUCCESS, bl, 1, "", request);
        } else {
            return new Response(MessageConstants.MSG_ACCESS_DENY, null, 0, "ID不正确", request);
        }
    }

    //删除缓存用户数据
    @ResponseBody
    @RequestMapping(value = "/delRedisUser")
    public Response delRedisUser(HttpServletRequest request, @RequestParam Map<String, Object> map) {
        int id = 0;
        try {
            id = Integer.parseInt(map.get("key").toString());
        } catch (Exception ex) {
        }
        if (id > 0) {
            Boolean bl = userRedisService.delRedisUser(id);
            return new Response(MessageConstants.MSG_SUCCESS, bl, 1, "", request);
        } else {
            return new Response(MessageConstants.MSG_ACCESS_DENY, null, 0, "ID不正确", request);
        }
    }

    //缓存用户数据
    @ResponseBody
    @RequestMapping(value = "/getRedisUser")
    public Response getRedisUser(HttpServletRequest request, @RequestParam Map<String, Object> map) {
        String key = map.get("key") == null ? null : map.get("key").toString();
        if (key != null) {
            System.out.println("Key：" + key);
            Object obj = redisTemplate.opsForValue().get(key);
            RedisUserNo redisUserNo;
            if (obj != null) {
                redisUserNo = JSON.parseObject(obj.toString(), new TypeReference<RedisUserNo>() {
                });
                if (redisUserNo != null) {
                    System.out.println("缓存：" + redisUserNo.getId());
                    return new Response(MessageConstants.MSG_SUCCESS, redisUserNo, 1, "", request);
                }
            }
        }
        return new Response(MessageConstants.MSG_ACCESS_DENY, null, 0, "读取缓存失败", request);
    }

    //保持缓存
    @Scheduled(fixedRate = 10000)
    public void AutoKeepRedisUser() {

        System.out.println("保持缓存");
        int maxid = 0;
        try {
            maxid = Integer.parseInt(redisTemplate.opsForValue().get("userjsmaxid").toString());
        } catch (Exception ex) {
        }
    }

}